var app = getApp();
Page({
    data:{
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isLogin:false
    },
    bindGetUserInfo: function (e) {
        var that=this;
        that.getUserInfo('',e);
    },
    getUserInfo: function(cb,res) {
        if(res.detail.errMsg=='getUserInfo:ok'){
            wx.setStorageSync('userinfo', res.detail.userInfo);
        }
        wx.login({
            success: function(r) {
                app.handlerPost('/api/account/doRegisterUser.do', {
                    code: r.code,
                    nickname: res.detail.userInfo.nickName,
                    headimgurl:res.detail.userInfo.avatarUrl,
                    openid:app.globalData.openid
                }).then( (res)=>{
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000,
                        success:function(){
                          wx.navigateTo({
                              url: '../index/index'
                            });
                        }
                    });
                }).catch( (errMsg)=>{
                    console.log(errMsg);
                } );
            }
        })
    },
    isLogin:function(){
        app.handlerPost('/api/account/doUserInfo.do',{userToken:app.globalData.openid},"GET").then( (res)=>{
            if(res.data.code=="SUCCESS"){
                 this.setData({
                     isLogin:true,
                     userInfo:res.data.data
                 });
            }
        }).catch( (errMsg)=>{
            console.log(errMsg);
        } );
    },
    onShow:function(){
        if(app.globalData.openid==''||app.globalData.openid==undefined){
            this.setData({
                isLogin:false
            });
            wx.login({
                success:function(res){
                    app.login({code:res.code});
                }
            });
        }
        this.isLogin();
    },
    onLoad: function() {

    },

});